import { defineStore } from 'pinia';

type HistoryItem = {
  data: string;
  description: string;
};

export const useHistoryStore = defineStore('history', {
  state: () => ({
    history: [] as HistoryItem[],
    currentIndex: -1,
  }),
  actions: {
    push(item: HistoryItem) {
      this.history = this.history.slice(0, this.currentIndex + 1);
      this.history.push(item);
      this.currentIndex = this.history.length - 1; // 确保索引始终指向最后
    },
// 修改undo方法
async undo() {
  if (this.currentIndex > 0) {
    this.currentIndex--;
    return this.history[this.currentIndex]; // 返回当前索引对应的状态
  }else if (this.currentIndex === 0) { // 处理最后一个可撤销项
    this.currentIndex = -1;
    return null; // 返回空状态
  }
  return null;
},
async redo() {
  if (this.currentIndex < this.history.length - 1) {
    this.currentIndex++;
    return this.history[this.currentIndex];
  }
  return null;
},
  },
});